package com.cskaoyan.wx.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bo.WxOrderCommentBo;
import com.cskaoyan.mapper.*;

import com.cskaoyan.wx.bean.bo.WxOrderSubmitBo;
import com.cskaoyan.wx.bean.vo.DetailVo;

import com.cskaoyan.wx.bean.vo.WxOrderSubmitVo;
import com.cskaoyan.wx.bean.vo.index.ListShowTypeVo;
import com.github.pagehelper.PageHelper;
import org.omg.CORBA.IRObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.spel.ast.OpOr;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @description:
 * @author: libugu@cskaoyan.com
 * @date 2021/4/12 - 17:10
 */
@Service
public class WxOrderServiceImpl implements WxOrderService {

    @Autowired
    CommentMapper commentMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    CartMapper cartMapper;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    ConfigMapper configMapper;

    @Override
    public int comment(WxOrderCommentBo wxOrderCommentBo) {

        Integer orderGoodsId = wxOrderCommentBo.getOrderGoodsId();

        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        orderGoodsExample.createCriteria().andIdEqualTo(orderGoodsId);
        List<OrderGoods> orderGoodsList1 = orderGoodsMapper.selectByExample(orderGoodsExample);
        Integer userId = 0;
        for (OrderGoods orderGoods : orderGoodsList1) {

            Integer orderId = orderGoods.getOrderId();
            Order order = orderMapper.selectByPrimaryKey(orderId);
            userId = order.getUserId();
        }

        Comment comment = new Comment();
        comment.setAddTime(new Date());
        comment.setUpdateTime(new Date());
        if (!wxOrderCommentBo.getHasPicture()) {
            comment.setPicUrls(null);
            comment.setHasPicture(false);
        }


        comment.setUserId(userId);
        comment.setPicUrls(wxOrderCommentBo.getPicUrls());
        comment.setHasPicture(true);
        comment.setStar(wxOrderCommentBo.getStar());
        comment.setContent(wxOrderCommentBo.getContent());
        comment.setValueId(wxOrderCommentBo.getOrderGoodsId());
        comment.setDeleted(false);
        comment.setType((byte) 0);



        OrderGoodsExample orderGoodsExample1 = new OrderGoodsExample();
        orderGoodsExample1.createCriteria().andIdEqualTo(orderGoodsId);
        List<OrderGoods> orderGoodsList = orderGoodsMapper.selectByExample(orderGoodsExample1);

        // xml需要改动
        int insert = commentMapper.insert(comment);


        for (OrderGoods orderGoods : orderGoodsList) {
            Integer id1 = comment.getId();
            orderGoods.setComment(id1);
            int i = orderGoodsMapper.updateByPrimaryKeySelective(orderGoods);
            if (i <= 0) {
                return 0;
            }
            return 1;
        }
        if (insert <= 0) {
            return 0;
        }
        return 1;
    }

    @Override
    public void cancel(Integer orderId) {
        // 未完善系统取消订单
        Order order = orderMapper.selectByPrimaryKey(orderId);
        order.setOrderStatus((short) 102);
        orderMapper.updateByPrimaryKeySelective(order);

    }

    @Override
    public void delete(Integer orderId) {

        orderMapper.deleteByPrimaryKey(orderId);

        orderGoodsMapper.deleteByOrderId(orderId);
    }

    @Override
    public DetailVo detail(Integer orderId) {

        DetailVo.OrderInfoBean orderInfoBean = new DetailVo.OrderInfoBean();
        Order order = orderMapper.selectByPrimaryKey(orderId);
        OrderGoods orderGoods = orderGoodsMapper.selectByPrimaryKey(orderId);
        orderInfoBean.setConsignee(order.getConsignee());
        orderInfoBean.setAddress(order.getAddress());
        orderInfoBean.setAddTime(order.getAddTime());
        orderInfoBean.setActualPrice(order.getActualPrice());
        orderInfoBean.setOrderSn(order.getOrderSn());
        orderInfoBean.setMobile(order.getMobile());
        Short orderStatus = order.getOrderStatus();
        DetailVo.OrderInfoBean.HandleOptionBean handleOptionBean = new DetailVo.OrderInfoBean.HandleOptionBean();

        String status = null;
        if (orderStatus == 101) {
            status = "未付款";

        } else if (orderStatus == 102 || orderStatus == 103) {
            status = "已取消";

        } else if (orderStatus == 201) {
            status = "已付款";

        } else if (orderStatus == 202) {
            status = "订单取消，退款中";
            ;
        } else if (orderStatus == 203) {
            status = "已退款";

        } else if (orderStatus == 301) {
            status = "已发货";

        } else if (orderStatus == 401 || orderStatus == 402) {
            status = "已收货";

        }
        orderInfoBean.setOrderStatusText(status);
        orderInfoBean.setGoodsPrice(order.getGoodsPrice());
        orderInfoBean.setCouponPrice(0);
        orderInfoBean.setId(orderId);
        orderInfoBean.setFreightPrice(0);

        if (orderStatus == 101) {

            handleOptionBean.setCancel(true);
            handleOptionBean.setComment(false);
            handleOptionBean.setConfirm(false);
            handleOptionBean.setDelete(false);
            handleOptionBean.setPay(true);
            handleOptionBean.setRebuy(false);
            handleOptionBean.setRefund(false);
            orderInfoBean.setHandleOption(handleOptionBean);

        } else if (orderStatus == 102) {

            handleOptionBean.setCancel(false);
            handleOptionBean.setComment(false);
            handleOptionBean.setConfirm(false);
            handleOptionBean.setDelete(true);
            handleOptionBean.setPay(false);
            handleOptionBean.setRebuy(false);
            handleOptionBean.setRefund(false);
            orderInfoBean.setHandleOption(handleOptionBean);

        } else if (orderStatus == 103) {

            handleOptionBean.setCancel(false);
            handleOptionBean.setComment(false);
            handleOptionBean.setConfirm(false);
            handleOptionBean.setDelete(true);
            handleOptionBean.setPay(false);
            handleOptionBean.setRebuy(false);
            handleOptionBean.setRefund(false);
            orderInfoBean.setHandleOption(handleOptionBean);
        } else if (orderStatus == 201) {

            handleOptionBean.setCancel(false);
            handleOptionBean.setComment(false);
            handleOptionBean.setConfirm(false);
            handleOptionBean.setDelete(false);
            handleOptionBean.setPay(false);
            handleOptionBean.setRebuy(false);
            handleOptionBean.setRefund(true);
            orderInfoBean.setHandleOption(handleOptionBean);
        } else if (orderStatus == 202) {

            handleOptionBean.setCancel(false);
            handleOptionBean.setComment(false);
            handleOptionBean.setConfirm(false);
            handleOptionBean.setDelete(false);
            handleOptionBean.setPay(false);
            handleOptionBean.setRebuy(false);
            handleOptionBean.setRefund(false);
            orderInfoBean.setHandleOption(handleOptionBean);
        } else if (orderStatus == 203) {

            handleOptionBean.setCancel(false);
            handleOptionBean.setComment(false);
            handleOptionBean.setConfirm(false);
            handleOptionBean.setDelete(true);
            handleOptionBean.setPay(false);
            handleOptionBean.setRebuy(true);
            handleOptionBean.setRefund(false);
            orderInfoBean.setHandleOption(handleOptionBean);
        } else if (orderStatus == 301) {

            handleOptionBean.setCancel(false);
            handleOptionBean.setComment(false);
            handleOptionBean.setConfirm(true);
            handleOptionBean.setDelete(false);
            handleOptionBean.setPay(false);
            handleOptionBean.setRebuy(false);
            handleOptionBean.setRefund(false);
            orderInfoBean.setHandleOption(handleOptionBean);
        } else if (orderStatus == 401) {

            handleOptionBean.setCancel(false);
            handleOptionBean.setComment(true);
            handleOptionBean.setConfirm(false);
            handleOptionBean.setDelete(true);
            handleOptionBean.setPay(false);
            handleOptionBean.setRebuy(true);
            handleOptionBean.setRefund(false);
            orderInfoBean.setHandleOption(handleOptionBean);
        } else if (orderStatus == 402) {

            handleOptionBean.setCancel(false);
            handleOptionBean.setComment(true);
            handleOptionBean.setConfirm(false);
            handleOptionBean.setDelete(true);
            handleOptionBean.setPay(false);
            handleOptionBean.setRebuy(true);
            handleOptionBean.setRefund(false);
            orderInfoBean.setHandleOption(handleOptionBean);
        }
        ArrayList<DetailVo.OrderGoodsBean> orderGoodsBeans = new ArrayList<>();


        //根据orderid找多goods
        //也有问题


        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        orderGoodsExample.createCriteria().andOrderIdEqualTo(orderId);
        List<OrderGoods> orderGoodsList = orderGoodsMapper.selectByExample(orderGoodsExample);

        for (OrderGoods goods : orderGoodsList) {
            DetailVo.OrderGoodsBean orderGoodsBean = new DetailVo.OrderGoodsBean();
            orderGoodsBean.setId(goods.getId());
            orderGoodsBean.setOrderId(goods.getOrderId());
            orderGoodsBean.setGoodsId(goods.getGoodsId());
            orderGoodsBean.setGoodsName(goods.getGoodsName());
            orderGoodsBean.setGoodsSn(goods.getGoodsSn());
            orderGoodsBean.setProductId(goods.getProductId());
            orderGoodsBean.setNumber(goods.getNumber());
            orderGoodsBean.setPrice(goods.getPrice());
            orderGoodsBean.setSpecifications(goods.getSpecifications());
            orderGoodsBean.setPicUrl(goods.getPicUrl());

            // 这个应该有问题
            orderGoodsBean.setComment(goods.getComment());
            orderGoodsBean.setAddTime(goods.getAddTime());
            orderGoodsBean.setUpdateTime(goods.getUpdateTime());
            orderGoodsBean.setDeleted(false);

            orderGoodsBeans.add(orderGoodsBean);
        }



        DetailVo detailVo = new DetailVo();
        detailVo.setOrderGoods(orderGoodsBeans);
        detailVo.setOrderInfo(orderInfoBean);

        return detailVo;


    }


    @Override
    public ListShowTypeVo list(Integer showType, Integer page, Integer size) {

        // 分页
        PageHelper.startPage(page, size);
        OrderExample orderExample = new OrderExample();
        ListShowTypeVo listShowTypeVo = new ListShowTypeVo();


        ListShowTypeVo.DataBean.HandleOptionBean handleOptionBean = new ListShowTypeVo.DataBean.HandleOptionBean();
        ArrayList<ListShowTypeVo.DataBean> dataBeans = new ArrayList<>();


        if (showType == 0) {
            long count = orderMapper.countByExample(orderExample);
            listShowTypeVo.setCount((int) count);
            int ceil = (int) Math.ceil((count / size));
            if (ceil > 0) {
                page = ceil + 1;
            } else {
                page = 0;
            }
            listShowTypeVo.setTotalPages(page);
            List<Order> orders = orderMapper.selectByExample(orderExample);


            for (Order order : orders) {
                ListShowTypeVo.DataBean dataBean = new ListShowTypeVo.DataBean();
                if (order.getOrderStatus() == 101) {
                    dataBean.setOrderStatusText("未付款");
                    dataBean.setIsGroupin(false);
                    dataBean.setOrderSn(order.getOrderSn());
                    dataBean.setActualPrice(order.getActualPrice());
                    dataBean.setId(order.getId());
                    handleOptionBean.setCancel(true);
                    handleOptionBean.setComment(false);
                    handleOptionBean.setConfirm(false);
                    handleOptionBean.setDelete(false);
                    handleOptionBean.setPay(true);
                    handleOptionBean.setRebuy(false);
                    handleOptionBean.setRefund(false);
                    dataBean.setHandleOption(handleOptionBean);


                } else if (order.getOrderStatus() == 102) {

                    dataBean.setOrderStatusText("用户取消");
                    dataBean.setIsGroupin(false);
                    dataBean.setOrderSn(order.getOrderSn());
                    dataBean.setActualPrice(order.getActualPrice());
                    dataBean.setId(order.getId());
                    handleOptionBean.setCancel(false);
                    handleOptionBean.setComment(false);
                    handleOptionBean.setConfirm(false);
                    handleOptionBean.setDelete(true);
                    handleOptionBean.setPay(false);
                    handleOptionBean.setRebuy(false);
                    handleOptionBean.setRefund(false);
                    dataBean.setHandleOption(handleOptionBean);

                } else if (order.getOrderStatus() == 103) {
                    dataBean.setOrderStatusText("系统取消");
                    dataBean.setIsGroupin(false);
                    dataBean.setOrderSn(order.getOrderSn());
                    dataBean.setActualPrice(order.getActualPrice());
                    dataBean.setId(order.getId());
                    handleOptionBean.setCancel(false);
                    handleOptionBean.setComment(false);
                    handleOptionBean.setConfirm(false);
                    handleOptionBean.setDelete(true);
                    handleOptionBean.setPay(false);
                    handleOptionBean.setRebuy(false);
                    handleOptionBean.setRefund(false);
                    dataBean.setHandleOption(handleOptionBean);
                } else if (order.getOrderStatus() == 201) {
                    dataBean.setOrderStatusText("已付款");
                    dataBean.setIsGroupin(false);
                    dataBean.setOrderSn(order.getOrderSn());
                    dataBean.setActualPrice(order.getActualPrice());
                    dataBean.setId(order.getId());
                    handleOptionBean.setCancel(false);
                    handleOptionBean.setComment(false);
                    handleOptionBean.setConfirm(false);
                    handleOptionBean.setDelete(false);
                    handleOptionBean.setPay(false);
                    handleOptionBean.setRebuy(false);
                    handleOptionBean.setRefund(true);
                    dataBean.setHandleOption(handleOptionBean);
                } else if (order.getOrderStatus() == 202) {
                    dataBean.setOrderStatusText("订单取消，退款中");
                    dataBean.setIsGroupin(false);
                    dataBean.setOrderSn(order.getOrderSn());
                    dataBean.setActualPrice(order.getActualPrice());
                    dataBean.setId(order.getId());
                    handleOptionBean.setCancel(false);
                    handleOptionBean.setComment(false);
                    handleOptionBean.setConfirm(false);
                    handleOptionBean.setDelete(false);
                    handleOptionBean.setPay(false);
                    handleOptionBean.setRebuy(false);
                    handleOptionBean.setRefund(false);
                    dataBean.setHandleOption(handleOptionBean);
                } else if (order.getOrderStatus() == 203) {
                    dataBean.setOrderStatusText("已退款");
                    dataBean.setIsGroupin(false);
                    dataBean.setOrderSn(order.getOrderSn());
                    dataBean.setActualPrice(order.getActualPrice());
                    dataBean.setId(order.getId());
                    handleOptionBean.setCancel(false);
                    handleOptionBean.setComment(false);
                    handleOptionBean.setConfirm(false);
                    handleOptionBean.setDelete(true);
                    handleOptionBean.setPay(false);
                    handleOptionBean.setRebuy(true);
                    handleOptionBean.setRefund(false);
                    dataBean.setHandleOption(handleOptionBean);
                } else if (order.getOrderStatus() == 301) {
                    dataBean.setOrderStatusText("已发货");
                    dataBean.setIsGroupin(false);
                    dataBean.setOrderSn(order.getOrderSn());
                    dataBean.setActualPrice(order.getActualPrice());
                    dataBean.setId(order.getId());
                    handleOptionBean.setCancel(false);
                    handleOptionBean.setComment(false);
                    handleOptionBean.setConfirm(true);
                    handleOptionBean.setDelete(false);
                    handleOptionBean.setPay(false);
                    handleOptionBean.setRebuy(false);
                    handleOptionBean.setRefund(false);
                    dataBean.setHandleOption(handleOptionBean);
                } else if (order.getOrderStatus() == 401) {
                    dataBean.setOrderStatusText("用户收货");
                    dataBean.setIsGroupin(false);
                    dataBean.setOrderSn(order.getOrderSn());
                    dataBean.setActualPrice(order.getActualPrice());
                    dataBean.setId(order.getId());
                    handleOptionBean.setCancel(false);
                    handleOptionBean.setComment(true);
                    handleOptionBean.setConfirm(false);
                    handleOptionBean.setDelete(true);
                    handleOptionBean.setPay(false);
                    handleOptionBean.setRebuy(true);
                    handleOptionBean.setRefund(false);
                    dataBean.setHandleOption(handleOptionBean);
                } else if (order.getOrderStatus() == 402) {
                    dataBean.setOrderStatusText("系统收货");
                    dataBean.setIsGroupin(false);
                    dataBean.setOrderSn(order.getOrderSn());
                    dataBean.setActualPrice(order.getActualPrice());
                    dataBean.setId(order.getId());
                    handleOptionBean.setCancel(false);
                    handleOptionBean.setComment(true);
                    handleOptionBean.setConfirm(false);
                    handleOptionBean.setDelete(true);
                    handleOptionBean.setPay(false);
                    handleOptionBean.setRebuy(true);
                    handleOptionBean.setRefund(false);
                    dataBean.setHandleOption(handleOptionBean);
                }


                Integer orderId = order.getId();
                OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
                orderGoodsExample.createCriteria().andOrderIdEqualTo(orderId);
                List<OrderGoods> orderGoodsList = orderGoodsMapper.selectByExample(orderGoodsExample);

                ArrayList<ListShowTypeVo.DataBean.GoodsListBean> goodsListBeans = new ArrayList<>();
                for (OrderGoods orderGoods : orderGoodsList) {

                    ListShowTypeVo.DataBean.GoodsListBean goodsListBean = new ListShowTypeVo.DataBean.GoodsListBean();

                    goodsListBean.setId(orderGoods.getId());
                    goodsListBean.setGoodsName(orderGoods.getGoodsName());
                    goodsListBean.setPicUrl(orderGoods.getPicUrl());
                    goodsListBean.setNumber(orderGoods.getNumber());
                    goodsListBeans.add(goodsListBean);


                }
                dataBean.setGoodsList(goodsListBeans);

                dataBeans.add(dataBean);

            }
            listShowTypeVo.setData(dataBeans);
            return listShowTypeVo;

        }


        if (showType == 1) {

            orderExample.createCriteria().andOrderStatusEqualTo((short) 101);
            long count = orderMapper.countByExample(orderExample);
            listShowTypeVo.setCount((int) count);
            int ceil = (int) Math.ceil(count / size);
            if (ceil > 0) {
                page = size + 1;
            } else {
                page = 0;
            }
            listShowTypeVo.setTotalPages(page);
            List<Order> orders = orderMapper.selectByExample(orderExample);


            for (Order order : orders) {
                ListShowTypeVo.DataBean dataBean = new ListShowTypeVo.DataBean();
                if (order.getOrderStatus() == 101) {
                    dataBean.setOrderStatusText("未付款");
                    dataBean.setIsGroupin(false);
                    dataBean.setOrderSn(order.getOrderSn());
                    dataBean.setActualPrice(order.getActualPrice());
                    dataBean.setId(order.getId());
                    handleOptionBean.setCancel(true);
                    handleOptionBean.setComment(false);
                    handleOptionBean.setConfirm(false);
                    handleOptionBean.setDelete(false);
                    handleOptionBean.setPay(true);
                    handleOptionBean.setRebuy(false);
                    handleOptionBean.setRefund(false);
                    dataBean.setHandleOption(handleOptionBean);
                }

                Integer orderId = order.getId();
                OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
                orderGoodsExample.createCriteria().andOrderIdEqualTo(orderId);
                List<OrderGoods> orderGoodsList = orderGoodsMapper.selectByExample(orderGoodsExample);

                ArrayList<ListShowTypeVo.DataBean.GoodsListBean> goodsListBeans = new ArrayList<>();
                for (OrderGoods orderGoods : orderGoodsList) {

                    ListShowTypeVo.DataBean.GoodsListBean goodsListBean = new ListShowTypeVo.DataBean.GoodsListBean();

                    goodsListBean.setId(orderGoods.getId());
                    goodsListBean.setGoodsName(orderGoods.getGoodsName());
                    goodsListBean.setPicUrl(orderGoods.getPicUrl());
                    goodsListBean.setNumber(orderGoods.getNumber());
                    goodsListBeans.add(goodsListBean);


                }
                dataBean.setGoodsList(goodsListBeans);

                dataBeans.add(dataBean);

            }
            listShowTypeVo.setData(dataBeans);
            return listShowTypeVo;
        }


        if (showType == 2) {
            orderExample.createCriteria().andOrderStatusEqualTo((short) 201);
            long count = orderMapper.countByExample(orderExample);
            listShowTypeVo.setCount((int) count);
            int ceil = (int) Math.ceil(count / size);
            if (ceil > 0) {
                page = size + 1;
            } else {
                page = 0;
            }
            listShowTypeVo.setTotalPages(page);
            List<Order> orders = orderMapper.selectByExample(orderExample);


            for (Order order : orders) {


                ListShowTypeVo.DataBean dataBean = new ListShowTypeVo.DataBean();

                if (order.getOrderStatus() == 201) {
                    dataBean.setOrderStatusText("已付款");
                    dataBean.setIsGroupin(false);
                    dataBean.setOrderSn(order.getOrderSn());
                    dataBean.setActualPrice(order.getActualPrice());
                    dataBean.setId(order.getId());
                    handleOptionBean.setCancel(false);
                    handleOptionBean.setComment(false);
                    handleOptionBean.setConfirm(false);
                    handleOptionBean.setDelete(false);
                    handleOptionBean.setPay(false);
                    handleOptionBean.setRebuy(false);
                    handleOptionBean.setRefund(true);
                    dataBean.setHandleOption(handleOptionBean);

                }
                Integer orderId = order.getId();
                OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
                orderGoodsExample.createCriteria().andOrderIdEqualTo(orderId);
                List<OrderGoods> orderGoodsList = orderGoodsMapper.selectByExample(orderGoodsExample);

                ArrayList<ListShowTypeVo.DataBean.GoodsListBean> goodsListBeans = new ArrayList<>();
                for (OrderGoods orderGoods : orderGoodsList) {

                    ListShowTypeVo.DataBean.GoodsListBean goodsListBean = new ListShowTypeVo.DataBean.GoodsListBean();

                    goodsListBean.setId(orderGoods.getId());
                    goodsListBean.setGoodsName(orderGoods.getGoodsName());
                    goodsListBean.setPicUrl(orderGoods.getPicUrl());
                    goodsListBean.setNumber(orderGoods.getNumber());
                    goodsListBeans.add(goodsListBean);


                }
                dataBean.setGoodsList(goodsListBeans);

                dataBeans.add(dataBean);

            }
            listShowTypeVo.setData(dataBeans);
            return listShowTypeVo;

        }


        if (showType == 3) {
            orderExample.createCriteria().andOrderStatusEqualTo((short) 301);
            long all = orderMapper.countByExample(orderExample);

//            long l = orderMapper.countByStatus(101);
//            long count = orderMapper.countByExample(orderExample);
            listShowTypeVo.setCount((int) all);
            listShowTypeVo.setTotalPages((int) Math.ceil(all / size));
            List<Order> orders = orderMapper.selectByExample(orderExample);


            for (Order order : orders) {

                ListShowTypeVo.DataBean dataBean = new ListShowTypeVo.DataBean();

                if (order.getOrderStatus() == 301) {
                    dataBean.setOrderStatusText("已发货");
                    dataBean.setIsGroupin(false);
                    dataBean.setOrderSn(order.getOrderSn());
                    dataBean.setActualPrice(order.getActualPrice());
                    dataBean.setId(order.getId());
                    handleOptionBean.setCancel(false);
                    handleOptionBean.setComment(false);
                    handleOptionBean.setConfirm(true);
                    handleOptionBean.setDelete(false);
                    handleOptionBean.setPay(false);
                    handleOptionBean.setRebuy(false);
                    handleOptionBean.setRefund(false);
                    dataBean.setHandleOption(handleOptionBean);


                }
                Integer orderId = order.getId();
                OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
                orderGoodsExample.createCriteria().andOrderIdEqualTo(orderId);
                List<OrderGoods> orderGoodsList = orderGoodsMapper.selectByExample(orderGoodsExample);

                ArrayList<ListShowTypeVo.DataBean.GoodsListBean> goodsListBeans = new ArrayList<>();
                for (OrderGoods orderGoods : orderGoodsList) {

                    ListShowTypeVo.DataBean.GoodsListBean goodsListBean = new ListShowTypeVo.DataBean.GoodsListBean();

                    goodsListBean.setId(orderGoods.getId());
                    goodsListBean.setGoodsName(orderGoods.getGoodsName());
                    goodsListBean.setPicUrl(orderGoods.getPicUrl());
                    goodsListBean.setNumber(orderGoods.getNumber());
                    goodsListBeans.add(goodsListBean);


                }
                dataBean.setGoodsList(goodsListBeans);

                dataBeans.add(dataBean);

            }
            listShowTypeVo.setData(dataBeans);
            return listShowTypeVo;

        }
        if (showType == 4) {
            orderExample.createCriteria().andOrderStatusEqualTo((short) 401);
            long all = orderMapper.countByExample(orderExample);

//            long l = orderMapper.countByStatus(101);
//            long count = orderMapper.countByExample(orderExample);
            listShowTypeVo.setCount((int) all);
            listShowTypeVo.setTotalPages((int) Math.ceil(all / size));
            List<Order> orders = orderMapper.selectByExample(orderExample);

            for (Order order : orders) {
                ListShowTypeVo.DataBean dataBean = new ListShowTypeVo.DataBean();
                if (order.getOrderStatus() == 401) {
                    dataBean.setOrderStatusText("用户收货");
                    dataBean.setIsGroupin(false);
                    dataBean.setOrderSn(order.getOrderSn());
                    dataBean.setActualPrice(order.getActualPrice());
                    dataBean.setId(order.getId());
                    handleOptionBean.setCancel(false);
                    handleOptionBean.setComment(true);
                    handleOptionBean.setConfirm(false);
                    handleOptionBean.setDelete(true);
                    handleOptionBean.setPay(false);
                    handleOptionBean.setRebuy(true);
                    handleOptionBean.setRefund(false);
                    dataBean.setHandleOption(handleOptionBean);

                } else if (order.getOrderStatus() == 402) {
                    dataBean.setOrderStatusText("系统收货");
                    dataBean.setIsGroupin(false);
                    dataBean.setOrderSn(order.getOrderSn());
                    dataBean.setActualPrice(order.getActualPrice());
                    dataBean.setId(order.getId());
                    handleOptionBean.setCancel(false);
                    handleOptionBean.setComment(true);
                    handleOptionBean.setConfirm(false);
                    handleOptionBean.setDelete(true);
                    handleOptionBean.setPay(false);
                    handleOptionBean.setRebuy(true);
                    handleOptionBean.setRefund(false);
                    dataBean.setHandleOption(handleOptionBean);

                }
                Integer orderId = order.getId();
                OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
                orderGoodsExample.createCriteria().andOrderIdEqualTo(orderId);
                List<OrderGoods> orderGoodsList = orderGoodsMapper.selectByExample(orderGoodsExample);

                ArrayList<ListShowTypeVo.DataBean.GoodsListBean> goodsListBeans = new ArrayList<>();
                for (OrderGoods orderGoods : orderGoodsList) {

                    ListShowTypeVo.DataBean.GoodsListBean goodsListBean = new ListShowTypeVo.DataBean.GoodsListBean();

                    goodsListBean.setId(orderGoods.getId());
                    goodsListBean.setGoodsName(orderGoods.getGoodsName());
                    goodsListBean.setPicUrl(orderGoods.getPicUrl());
                    goodsListBean.setNumber(orderGoods.getNumber());
                    goodsListBeans.add(goodsListBean);


                }
                dataBean.setGoodsList(goodsListBeans);

                dataBeans.add(dataBean);

            }
        }
        listShowTypeVo.setData(dataBeans);
        return listShowTypeVo;
    }


    @Override
    public void refund(Integer orderId) {
        Order order = orderMapper.selectByPrimaryKey(orderId);
        order.setOrderStatus((short) 203);
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public void confirm(Integer orderId) {
        Order order = orderMapper.selectByPrimaryKey(orderId);
        order.setOrderStatus((short) 401);
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public OrderGoods goods(Integer orderId, Integer goodsId) {

        OrderGoods orderGoods1 = new OrderGoods();
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        orderGoodsExample.createCriteria().andOrderIdEqualTo(orderId).andGoodsIdEqualTo(goodsId);
        List<OrderGoods> orderGoodsList = orderGoodsMapper.selectByExample(orderGoodsExample);
        for (OrderGoods orderGoods : orderGoodsList) {
            orderGoods1=orderGoods;
            return orderGoods1;
        }
        return orderGoods1;


    }

    @Override
    public WxOrderSubmitVo submit(WxOrderSubmitBo wxOrderSubmitBo, String username) {

        Order order = new Order();

        //先查询用户信息
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);

        if (users == null || users.size() <= 0) {
            return null;
        }

        User user = users.get(0);

        //处理orderBean
        order.setUserId(user.getId());
        Random random = new Random();
        int i = random.nextInt(1000000);
        order.setOrderSn(i + "");
        order.setOrderStatus((short) 101);

        Integer addressId = wxOrderSubmitBo.getAddressId();
        Address address = getAddress(addressId);
        order.setConsignee(address.getName());
        order.setMobile(address.getMobile());
        order.setAddress(address.getAddress());
        order.setMessage(wxOrderSubmitBo.getMessage());
        order.setDeleted(false);

        //找到购物车中的商品
        CartExample cartExample = new CartExample();
        cartExample.createCriteria().andDeletedEqualTo(false).andUserIdEqualTo(user.getId()).andCheckedEqualTo(true);
        List<Cart> carts = cartMapper.selectByExample(cartExample);

        BigDecimal goodsPrice = new BigDecimal(0);

        order.setGoodsPrice(goodsPrice);

        //查询运费
        BigDecimal freightPrice = configMapper.selectFreightPrice();
        order.setFreightPrice(freightPrice);

        //其他价格默认值
        BigDecimal defaultPrice = new BigDecimal(0);

        order.setCouponPrice(defaultPrice);
        order.setIntegralPrice(defaultPrice);
        order.setGrouponPrice(defaultPrice);
        order.setOrderPrice(goodsPrice.add(freightPrice));
        order.setActualPrice(goodsPrice.add(freightPrice));

        order.setComments((short) 0);

        order.setAddTime(new java.sql.Date(System.currentTimeMillis()));
        order.setUpdateTime(new java.sql.Date(System.currentTimeMillis()));

        int insert = orderMapper.insert(order);

        if (insert <= 0) {
            return null;
        }

        //取出orderId
        Integer orderId = order.getId();

        //将购物车中的商品插入到order_goods表中
        for (Cart cart : carts) {

            BigDecimal number = new BigDecimal(cart.getNumber());
            goodsPrice = goodsPrice.add(cart.getPrice().multiply(number));

            OrderGoods orderGoods = OrderGoods.cartToOrderGoods(cart, orderId);
            int insertGoods = orderGoodsMapper.insert(orderGoods);
            if (insertGoods <= 0) {
                throw new RuntimeException("订单插入商品失败");
            }

            cart.setDeleted(true);
            cartMapper.updateByPrimaryKey(cart);

        }


        Order orderP = orderMapper.selectByPrimaryKey(orderId);
        orderP.setGoodsPrice(goodsPrice);
        BigDecimal goodsPrice1 = orderP.getGoodsPrice();
        BigDecimal actualPrice = orderP.getActualPrice();
        orderP.setActualPrice(actualPrice.add(goodsPrice1));
        orderMapper.updateByPrimaryKey(orderP);

        WxOrderSubmitVo wxOrderSubmitVo = new WxOrderSubmitVo();
        wxOrderSubmitVo.setOrderId(orderId);
        return wxOrderSubmitVo;
    }

    @Override
    public int prepay(Integer orderId) {

        Order order = orderMapper.selectByPrimaryKey(orderId);
        if (order == null) {
            return 400;
        }
        order.setOrderStatus((short) 201);
        int i = orderMapper.updateByPrimaryKey(order);
        if (i <= 0) {
            return 404;
        }
        return 200;
    }

    private Address getAddress(Integer addressId) {

        return addressMapper.selectByPrimaryKey(addressId);

    }
}

